package com.example.controleautomovel.activity;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileOutputStream;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.location.LocationManager;
import android.os.Bundle;
import android.os.Environment;
import android.provider.MediaStore;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RadioGroup;
import android.widget.TextView;
import android.widget.Toast;

import com.example.controleautomovel.R;
import com.example.controleautomovel.adapter.AutomovelDBAdapter;
import com.example.controleautomovel.pojo.Automovel;

public class NovoAutomovelActivity extends Activity {

//	@Override
//	protected void onCreate(Bundle savedInstanceState) {
//		super.onCreate(savedInstanceState);
//		setContentView(R.layout.activity_novo_automovel);
//	}
//
//	@Override
//	public boolean onCreateOptionsMenu(Menu menu) {
//		// Inflate the menu; this adds items to the action bar if it is present.
//		getMenuInflater().inflate(R.menu.novo_automovel, menu);
//		return true;
//	}

	
	EditText txtNome;
	EditText txtPlaca;
	EditText txtAno;
	EditText txtChassi;
	EditText txtMarca;
	EditText txtModelo;
	CheckBox checkPrincipal;
	RadioGroup radioTipoOdometro;
	String fotoNome;
	
	LocationManager locManager = null;
	TextView txtLatitude = null;
	TextView txtLongitude = null;
	
	public static final int REQUISICAO_CAMERA = 100;
//	ImageView imgFotoCamera = null;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_novo_automovel);
		
		txtNome = (EditText) findViewById(R.id.txtNome);
		txtPlaca = (EditText) findViewById(R.id.txtPlacaAutomovel);
		txtAno = (EditText) findViewById(R.id.txtAnoAutomovel);
		txtChassi = (EditText) findViewById(R.id.txtChassi);
		txtMarca = (EditText) findViewById(R.id.txtMarca);
		txtModelo = (EditText) findViewById(R.id.txtModelo);
		checkPrincipal = (CheckBox) findViewById(R.id.checkPrincipal);
		radioTipoOdometro = (RadioGroup) findViewById(R.id.radioGroupAbastecimento);
	}

	public void iniciaCamera(View v){
		Intent it = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
		startActivityForResult(it, REQUISICAO_CAMERA);
	}
	
	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		// TODO Auto-generated method stub
		super.onActivityResult(requestCode, resultCode, data);
		
		if(resultCode != RESULT_OK){
			return;
		}
		
		if(requestCode == REQUISICAO_CAMERA){
			Toast.makeText(getBaseContext(), "Veio Foto!", Toast.LENGTH_SHORT).show();
			Bitmap imagemCamera = (Bitmap) data.getExtras().get("data");
//			imgFotoCamera.setImageBitmap(imagemCamera);
			
			try{
				ByteArrayOutputStream bytes = new ByteArrayOutputStream();
				imagemCamera.compress(Bitmap.CompressFormat.PNG, 80, bytes);
				
				File dir = new File(Environment.getExternalStorageDirectory().getAbsolutePath() + "/fotos");
				if(!dir.exists()){
					dir.mkdir();
				}
				
				fotoNome = "/fotos/foto"+txtNome.getText().toString()+txtPlaca.getText().toString()+".png";
				File arquivo = new File(Environment.getExternalStorageDirectory().getAbsolutePath() + fotoNome );
				FileOutputStream out = new FileOutputStream(arquivo);
				out.write(bytes.toByteArray());
				out.close();
				Log.d("file", "Salvo");
			}catch(Exception e){
				e.printStackTrace();
			}
			
		}
	}
	
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.novo_automovel, menu);
		return true;
	}

	
	public void adicionarAutomovel(View v){
		String nome = txtNome.getText().toString();
		String placa = txtPlaca.getText().toString();
		String ano = txtAno.getText().toString();
		String chassi = txtChassi.getText().toString();
		String marca = txtMarca.getText().toString();
		String modelo = txtModelo.getText().toString();
		String principal = checkPrincipal.isChecked() ? "1" : "0";
		String tipoOdometro = radioTipoOdometro.getCheckedRadioButtonId() == R.id.radioKm ? "km" : "mi";
		String foto = fotoNome;
		
		txtNome.setText("");
		txtPlaca.setText("");
		txtAno.setText("");
		txtChassi.setText("");
		txtMarca.setText("");
		txtModelo.setText("");
		
		Automovel c = new Automovel();
		c.setNome(nome);
		c.setPlaca(placa);
		c.setAno(ano);
		c.setChassi(chassi);
		c.setMarca(marca);
		c.setModelo(modelo);
		c.setFoto(foto);
		c.setPrincipal(principal);
		c.setTipoOdometro(tipoOdometro);
		
		AutomovelDBAdapter db = new AutomovelDBAdapter(this);
		db.open();
		db.inserirAutomovel(c);
		db.close();
		
		finish();
	}
}
